function getProdUrl() {
  const { pathname } = window.location
  const index = pathname.indexOf('/view/')
  return pathname.slice(0, index)
}

export const API_BASE = import.meta.env.DEV ? '/api/' : `${getProdUrl()}${import.meta.env.VITE_GATEWAY}`

const ApiPrefixMap = {
  SERVICE: {
    label: '用户中心',
    value: '/qy-user-service',
  },
  LZM: {
    label: '本地开发',
    value: '/qy-user-lzm',
  },
  GATEWAY: {
    label: '默认网关',
    value: '/gateway',
  },
  SIGNATURE: {
    label: '签署服务',
    value: '/signature',
  },
  CONTRACT: {
    label: '合同服务',
    value: '/qy-contract',
  },
  EXPERT: {
    label: '新闻公告服务',
    value: '/oa-expert',
  },
  OA: {
    label: 'OA服务',
    value: '/qy-contract',
  },
  FILE: {
    label: '文件服务',
    value: '/qy-file',
  },
  RUNTIME: {
    label: '运行时',
    value: '/qy-runtime',
  },
} as const

export type ApiPrefix = keyof typeof ApiPrefixMap
export type ApiPrefixValue = (typeof ApiPrefixMap)[ApiPrefix]['value']

function getApiPrefix(): Record<ApiPrefix, ApiPrefixValue | string> {
  if (import.meta.env.VITE_DEPLOY_ENV === 'PUBLIC') {
    return Object.fromEntries(
      Object.entries(ApiPrefixMap).map(([key, value]) => [key, value.value]),
    ) as Record<ApiPrefix, ApiPrefixValue | string>
  }
  return Object.fromEntries(
    Object.keys(ApiPrefixMap).map(key => [key, '']),
  ) as Record<ApiPrefix, ApiPrefixValue | string>
}

export const ApiPrefixEnum = getApiPrefix()
